/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sapra.echo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.ContentPane;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.TextArea;
import nextapp.echo2.app.WindowPane;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.text.Document;
import nextapp.echo2.app.text.StringDocument;
import nextapp.echo2.extras.app.CalendarSelect;
import org.sapra.abc.vABCP;
import org.sapra.abc.vPracticante;
import org.sapra.logon.dLogin;

/**
 *
 * @author luiz
 */
public class Calendario extends ContentPane {
    private int opc=0;
    private vABCP vista;
    private CalendarSelect cal_Select=new CalendarSelect();
    private Button Seleccionar;
    private SimpleDateFormat spd=new SimpleDateFormat("yyyy-MM-dd");
    private vPracticante princ;
    private TextArea area_txt;

    public Calendario(vABCP win,int opc){
        super();
        this.opc=opc;
        this.vista=win;
        initComponents();
    }
    public Calendario(vPracticante princ,int opc){
        super();
        this.princ=princ;
        this.opc=opc;
        initComponents();
    }
   


    public void initComponents(){
            
            Column c=new Column();
            Row linea1=new Row();
            Row linea2=new Row();
            cal_Select.setBackground(new Color(224,255,255));
            cal_Select.setForeground(Color.BLACK);
            cal_Select.setSelectedDateBackground(new Color(255,215,0));
            cal_Select.setSelectedDateForeground(Color.BLUE);
            Seleccionar=new Button("Seleccionar");
            Seleccionar.setBackground(Color.DARKGRAY);
            Seleccionar.setForeground(Color.WHITE);
            Seleccionar.setWidth(new Extent(100));
            if(opc!=5){
                cal_Select.setDate(vista.getFechaInicio());

                linea1.add(cal_Select);
                linea2.add(Seleccionar);
                linea1.setAlignment(Alignment.ALIGN_CENTER);
                linea2.setAlignment(Alignment.ALIGN_CENTER);


                c.add(linea1);
                c.add(linea2);

            }else{
                area_txt = new TextArea(new StringDocument(), "SIN COMENTARIO", 20, 10);
                area_txt.setWidth(new Extent(300));
                area_txt.setHeight(new Extent(100));
                Grid g=new Grid(2);
                g.add(new Label("Fecha de Baja"));
                g.add(new Label("Motivo de Baja"));
                g.add(cal_Select);
                g.add(area_txt);
                c.add(g);
                linea2.add(Seleccionar);
                linea2.setAlignment(Alignment.ALIGN_CENTER);

                c.add(linea2);
                c.setInsets(new Insets(20, 10, 0, 0));
                
            }
             add(c);
            
            if(opc==1){
                cal_Select.setDate(vista.getFechaInicio());
                accion1();
            }
                
            if(opc==2){
                cal_Select.setDate(vista.getFechaInicio());
                accion2();
            }
                
            if(opc==3){
                cal_Select.setDate(vista.getFechaFin());
                accion3();
            }
            if(opc==4){
                cal_Select.setDate(new Date());
                accion4();
            }
            if(opc==5){
                cal_Select.setDate(new Date());
                accion5();
            }
                
    }

    public void accion1(){
            Seleccionar.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                        try {
                                Calendar fechafin = GregorianCalendar.getInstance();
                                int i=vista.getmListPFCerrada().getSelectedIndex();
                                if(i>=0){                           
                                    fechafin.setTimeInMillis(cal_Select.getDate().getTime());
                                    fechafin.add(Calendar.MONTH,i+1);
                                    
                                }
                                vista.setFechaInicio(cal_Select.getDate());
                                vista.getmFInicio().setText(spd.format(cal_Select.getDate()));
                                if(i>=0){
                                    vista.setFechaFin(fechafin.getTime());
                                    vista.getmFTermino().setText(spd.format(fechafin.getTime()));
                                    if(dLogin.getEmpresa().getPracticantes_seguro()==true){
                                            if(vista.getmAction() !=  vista.EDITAR){
                                                vista.getSegInicio().setText(spd.format(cal_Select.getDate()));
                                                vista.getSegTermino().setText(spd.format(fechafin.getTime()));
                                            }
                                            
                                    }
                                    
                                }
                                vista.setVentInicio(0);
                                WindowPane parent = (WindowPane)getParent();
                                parent.userClose();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });

    }

    public void accion2(){
            Seleccionar.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                        try {
                                vista.setFechaInicio(cal_Select.getDate());
                                vista.getmFInicio().setText(spd.format(cal_Select.getDate()));
                                vista.setVentInicio(0);
                                vista.fechaAbierta(0);
                                WindowPane parent = (WindowPane)getParent();
                                parent.userClose();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });
    }
    public void accion3(){
        Seleccionar.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                 try {
                            vista.setFechaFin(cal_Select.getDate());
                            vista.getmFTermino().setText(spd.format(cal_Select.getDate()));
                            vista.setVentFin(0);
                            vista.fechaAbierta(0);
                            WindowPane parent = (WindowPane)getParent();
                            parent.userClose();
                    } catch (Exception ex) {
                    }
            }
        });
                   

    }
    public void accion4(){
        Seleccionar.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                 try {
                            vista.getmNatalicio().setText(spd.format(cal_Select.getDate()));
                            vista.setVentFin(0);
                            WindowPane parent = (WindowPane)getParent();
                            parent.userClose();
                    } catch (Exception ex) {
                    }
            }
        });


    }

    public void accion5(){
        Seleccionar.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                WindowPane parent = (WindowPane)getParent();
                parent.userClose();
                princ.ProcBaja(cal_Select.getDate(),area_txt.getText());
            }
        });

    }
}
